package 操作系统实验.实验三.第二版;/**
 * ************************************************************************
 * 项目名称: projectweb <br>
 * 文件名称:  <br>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br>
 * 文件创建：1913040634刘世锦  <br>
 * 创建时间: 2021/4/20 <br>
 *
 * @version v1.0 <br>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br>
 * ************************************************************************
 */

/**
 * @ProjectName: projectweb
 * @ClassName: Banker
 * @Description: 请描述该类的功能
 * @Author: 1913040634刘世锦
 * @Date: 2021/4/20 12:50 .
 * @version v1.0
 */
import java.util.Scanner;

public class Banker {
    private Integer[][] request;  //进程的请求向量
    private Integer[] available;  //可利用资源向量
    /**
     * 银行家算法
     */
    public void Bank(){
        Processes processes = new Processes();
        Scanner input = new Scanner(System.in);
        request = new Integer[processes.getPROCESSES_COUNT()][processes.getAVAILABLE_COUNT_Y()];
        available = new Integer[processes.getAVAILABLE_COUNT_Y()];
        //给请求向量赋值
        System.out.println("请输入各进程对各类资源分别的请求量：");
        for (int i = 0; i < processes.getPROCESSES_COUNT(); i++) {
            System.out.println("进程" + (i+1));
            for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) {
                System.out.print(processes.getResourceName(j) + "：");
                request[i][j] = input.nextInt();
            }
        }
        //给系统可利用资源向量赋值
        System.out.println("请输入各类可利用的资源数目：");
        for (int i = 0; i < processes.getAVAILABLE_COUNT_Y(); i++) {
            System.out.print(processes.getResourceName(i) + "：");
            available[i] = input.nextInt();
        }
        //判断资源是否能都满足请求
        for (int i = 0; i < processes.getPROCESSES_COUNT(); i++) {
            System.out.println("进程" + (i+1) + "开始进行请求。。。");
            for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) {
                if (request[i][j] <= processes.getNeedNum(i,j)){
                    if (request[i][j] <= available[j]){

                        System.out.println("请求暂时成功，正在进行安全性检查。。。。。。");
                        //请求暂时成功进行安全性检查


                        available[j] = available[j] - request[i][j];
                        processes.setAllocation(i,j,processes.getAllocation(i,j) + request[i][j]);
                        processes.setNeedNum(i,j,processes.getNeedNum(i,j) - request[i][j]);
                        System.out.println("P" + (i+1) + "分配成功！");
                        break;
                    }else {
                        System.err.println("尚无足够的资源，P" + (i+1) + "需等待");
                        continue;
                    }
                }else {
                    System.err.println("请求出错，原因：P" + (i+1) + "进程所需要" + processes.getResourceName(j) + "类资源数已超过它所宣布最大值");
                    continue;
                }
            }
            System.out.println();
        }
        Safity(processes);
    }

    /**
     * 安全性算法
     */
    public void Safity(Processes processes){
        Integer[] work = new Integer[processes.getAVAILABLE_COUNT_Y()];     //工作向量Work
        work = available;
        String[] finish = new String[processes.getPROCESSES_COUNT()];   //工作向量Finish
        for (int j = 0; j < finish.length; j++) {
            finish[j] = "false";
        }
        boolean flag = true;    //是否运行完成标志
        for (int i = 0; i < processes.getPROCESSES_COUNT(); i++) {
            for (int j = 0; j < processes.getAVAILABLE_COUNT_Y(); j++) {
                if (finish[i].compareTo("false") == 0  &&  processes.getNeedNum(i,j) <= work[j]){
                    work[j] = work[j] + processes.getAllocation(i,j);
                    finish[i] = "true";

                }else {

                    for (int k = 0; k < processes.getPROCESSES_COUNT(); k++) {
                        if (finish[k].compareTo("true") != 0){
                            flag = false;
                        }
                    }
                }
            }
        }

        if (flag)
            System.out.println("系统处于安全状态");
        else
            System.out.println("系统处于不安全状态");
    }
}

